/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.rainbowsheep.reisorg.rest.service.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.rainbowsheep.reisorg.rest.service.model.Language;

/**
 *
 * @author ieme
 *
 *
 *
 */
public class Localizer {
    
    private static Language currentLanguage;

    public static Language getCurrentLanguage() {
        return currentLanguage;
    }

    public static void setCurrentLanguage(Language currentLanguage) {
        Localizer.currentLanguage = currentLanguage;
    }

    public static Object getLocalizedObject(Map localizedMap, Class clazz) {   
        if(localizedMap == null) {
            localizedMap = new HashMap();
        }
        Object localizedObject = null;
        Iterator it = localizedMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Language languageToCheck = (Language) pairs.getKey();
            if (currentLanguage.getLanguageName().trim().equals(languageToCheck.getLanguageName().trim())) {
                localizedObject = pairs.getValue();
                break;
            }
        }
        if(localizedObject == null) {
            try {
                localizedObject = Class.forName(clazz.getName()).newInstance();
                localizedMap.put(currentLanguage, localizedObject);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Localizer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(Localizer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Localizer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return localizedObject;
    }
}
